package Chapter06;

/**
 * 使用守护线程
 * java中的线程分为两类,即用户线程和守护线程,用户线程指的是完成用户的任务,而守护线程
 * 则是为线程提供服务.
 * 守护线程会随时中断,因此不要在守护线程上使用需要释放资源的资源，
 * 所有的守护线程都是后台线程,如果虚拟机中只剩下守护线程,虚拟机就会退出.
 * Created by a on 2017/6/11.
 */
public class Tets128 {
    public static void main(String[] args) {
//        System.out.println(Thread.currentThread().isDaemon());
        Thread userTherad = new Thread(new Worker());
        Thread daemonThread = new Thread(new Timer());
        daemonThread.setDaemon(true);
        userTherad.start();
        daemonThread.start();


    }


}

class Worker implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i<5;i++){
            System.out.println("Java编程词典第"+i+"次更新!");
        }
    }
}
class Timer implements Runnable {

    @Override
    public void run() {
        long currenTime = System.currentTimeMillis();
        long processTime = 0;
        while (true){
            if ((System.currentTimeMillis() - currenTime)>processTime){
                processTime = System.currentTimeMillis()-currenTime;
                System.out.println("程序运行时间:"+processTime);
            }
        }
    }
}
